package sjtu.ist.warehouse4ws.action.file;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.struts2.json.annotations.JSON;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.model.Oplog;
import sjtu.ist.warehouse4ws.model.ShiroUser;
import sjtu.ist.warehouse4ws.security.MyRealm;
import sjtu.ist.warehouse4ws.service.OplogService;
import sjtu.ist.warehouse4ws.service.ShiroUserService;
import sjtu.ist.warehouse4ws.tool.BaseAction;
import sjtu.ist.warehouse4ws.tool.MyUtils;

import com.opensymphony.xwork2.ActionContext;

public class SLAUploadAction extends BaseAction {	
	private final String ROOT="upload\\";
	
	private ShiroUserService shiroUserService;
	public MyRealm myRealm;
	
	private RepositoryMoudl repositoryMoudl;
	private OplogService oplogService;

	private String myUploadFileName;
	private String userName;
	private String modelgroupId;
	private File myUpload;
	
	private boolean success=true;

	/**
	 * 上传sla文件
	 * 
	 * @return
	 * @throws SAXException
	 * 
	 * @throws IOException
	 */
	public String uploadSLA() {
		System.out.println("upload sla");
		
		String rootPath = getSession().getServletContext().getRealPath("/");
		rootPath += ROOT;
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		securityManager.setRealm(myRealm);
		SecurityUtils.setSecurityManager(securityManager);

		ShiroUser user = shiroUserService.findUser(userName);
		int companyId = user.getCompanyId();

		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		session.put("companyId", companyId);

		String uploadFileName = myUploadFileName;
		int last = uploadFileName.lastIndexOf(".");
		String sp = rootPath;
		String group = uploadFileName.substring(0, last);
		
		MyUtils.mkDirectory(sp);	
		
		MyUtils.upload(uploadFileName, sp, myUpload);
		
		List<String> fileNameList=new ArrayList<String>();
		fileNameList.add(myUpload.getAbsolutePath());
		
		//存入模型库
		repositoryMoudl.addServiceFile(String.valueOf(companyId), "sla", null, uploadFileName, fileNameList);
		//提取模型版本号
		//String version=repositoryMoudlService.getVersion(String.valueOf(companyId),group);
		
		//插入记录
		Oplog oplog=new Oplog();
		oplog.setGroupId(modelgroupId);
		oplog.setUser(userName);
		oplog.setOperation(Oplog.upload);
		oplog.setDiscription("上传sla文件");
		oplogService.insertOplog(oplog);
		
		System.out.println("upload sla over");
		
		return SUCCESS;

	}

	public void setMyUploadFileName(String myUploadFileName) {
		this.myUploadFileName = myUploadFileName;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}


	public void setModelgroupId(String modelgroupId) {
		this.modelgroupId = modelgroupId;
	}


	public void setMyUpload(File myUpload) {
		this.myUpload = myUpload;
	}


	public void setMyRealm(MyRealm myRealm) {
		this.myRealm = myRealm;
	}

	/**
	 * @param shiroUserService
	 *            the shiroUserService to set
	 */
	public void setShiroUserService(ShiroUserService shiroUserService) {
		this.shiroUserService = shiroUserService;
	}
	
	public void setRepositoryMoudl(RepositoryMoudl repositoryMoudl) {
		this.repositoryMoudl = repositoryMoudl;
	}

	public void setOplogService(OplogService oplogService) {
		this.oplogService = oplogService;
	}
}
